let parentId = 0
let productId=""
let id = 0
let number=0
let n = 0
let pro = 0
$(function () {
    $("#gradeTable").bootstrapTable({
        method: "get",
        contentType: "application/json",
        url: "/inputmanagerController/getSpaycheckTag1storeTag1",
        // striped: true,
        pageNumber: 1,
        pagination: true,
        sidePagination: 'server',
        pageSize: 5,
        pageList: [1, 3, 5, 7],
        queryParams: function (parmes) {
            return {
                pageNum: (parmes.offset / parmes.limit) + 1,
                pageSize: parmes.limit,
                storer: $("#storer").val(),
                payId: $("#gatherId").val(),
                registerTime: $("#registerTime").val(),
                checker: $("#checker").val()
            }
        },
        columns: [
            { title: "申请单编号", field: 'payId', align: "center" },
            { title: "出库理由", field: 'reason', align: "center" },
            { title: "出库详细理由", field: 'reasonexact', align: "center",
                formatter: function (value,row,index) {
                   if(value==undefined){
                       return "暂无留言"
                   }else {
                       return  value
                   }
                }},
            { title: "登记时间", field: 'registerTime', align: "center" },
            { title: "总计数", field: 'amountSum', align: "center" },
            { title: "总金额", field: 'costPriceSum', align: "center" },
            {
                title: "出库调度", field: 'gatherId', align: "center",
                formatter: function (value,row,index) {
                    var btn1 = "<button class=\"btn btn-success btn-sm\" onclick='checkTag("+value+","+JSON.stringify(row)+")'>出库调度</button>";
                    return btn1
                }
            }
        ]
    })
    $("#condition").click(function(){
        $("#gradeTable").bootstrapTable("refresh")
    })
})
//审核  跳页面 把该出库单 和明细显示出来
function checkTag(gatherId,parse) {
    console.log(parse)
    $("#rkdbh").html(parse.payId)                    //出库单编号
    $("#rkxqly").html(parse.reasonexact)          //出库详情理由
    $("#rkly").html(parse.reason)                   //出库理由
    $("#two").show()
    $("#one").hide()
    $("#rkr").val(parse.storer)
    $("#rkly").val(parse.reason)
    $("#zjs").html(parse.amountSum)
    $("#zje").html(parse.costPriceSum)
    $("#djr").html(parse.register)
    $("#djsj").html(parse.registerTime)
    $("#bz").val(parse.remark)
    parentId=parse.id
    $.ajax({
        url:"/inputmanagerController/ByPayIdquerySayDetails",
        type:"get",
        contentType: "application/json",
        async:true,
        data:{
            id:parse.id,
        },
        success:function (list) {
            console.log(list)
            let content="";
            for (let i = 0; i < list.length; i++) {
                if(list[i].productDescribe==undefined){
                    list[i].productDescribe=""
                }
                productId=list[i].productId  //从产品id
                id = list[i].id //入库明细id
                content += "<tr>" +
                    "<td>"+ list[i].id +"</td>" +
                    "<td>" + list[i].productName + "</td>" +
                    "<td>" + list[i].productId + "</td>" +
                    "<td>" + list[i].productDescribe + "</td>" +
                    "<td>" + list[i].amount + "</td>" +
                    "<td>" + 0.0 + "</td>" +//list[i].gatheredAmount
                    "<td><a class='butt' href='#' data-toggle=\"modal\" data-target=\"#myModal\" onclick='querysCellCurrentAmount("+list[i].productId+","+list[i].amount+","+list[i].id+","+i+")'>调度</a></td>" +
                    "</tr>"
            }
            $("#tbody").append(content)
        }
    })
}
//审核通过与不通过
function pass(checkTag) {
    $.ajax({
        url:"/inputmanagerController/updateSPaycheckTag",
        type:"get",
        contentType: "application/json",
        async:true,
        data:{
            id:parentId,
            checkTag:checkTag
        },
        success:function (data) {
            console.log(data)
        }
    })
}

function demand() {
    $("#gradeTable").bootstrapTable("refresh")
}
//'ByproductIdqueryIsdesignCellTag("+list[i].productId+","+list[i].id+")'
//点击确定  根据产品明细中的产品id  查询档案表该产品id是否  有设计库存分配标准（design_cell_tag） 是否设计  如果  未设计（0）不能调度  设计（1）修改相关信息
function ByproductIdqueryIsdesignCellTag() {
    console.log(productId,id)
    let reduceAmount =$("#shouldAmount").val()
    if(reduceAmount==""){
        alert("出库数量不可为空")
        return;
    }
    let reduceAmounts =parseInt(reduceAmount)
    if(reduceAmounts<0){
        alert("出库数量不可为负数")
        return;
    }
    if(reduceAmount>parseInt($("#mayAmount").html())){
        alert("本次出库数量不可大于当前存放数量")
        return
    }
    if(number!=reduceAmount){
        alert("出库数量与应出库数量不一致请重新输入")
        return;
    }
    $.ajax({
        url:"/inputmanagerController/ByproductIdqueryPayIsdesignCellTag",
        type:"get",
        contentType: "application/json",
        async:true,
        data:{
            reduceAmount:reduceAmount,
            parentId:parentId,
            id:id,
            productId:pro,
        },
        success:function (data) {
            console.log(data)
           // $(".butt")[n].disabled=true
            $(".butt")[n].innerHTML="已调度"
            $(".butt")[n].removeAttribute("data-target")
            if(data=="1"){
                alert("调度成功")
            }else {
                alert("对不起，该产品尚未分配存储空间，请返回")
            }
        }
    })
}

//点击调度 根据产品id查询 库存单元表 该商品的当前剩余存储量
function querysCellCurrentAmount(productId,numbe,i,index) {
    //每次点调度时清空上一次的 当前存放数量
    $("#mayAmount").html("")
    $.ajax({
        url:"/inputmanagerController/ByproductIdqueryIsdesignCellTag2",
        type:"get",
        contentType: "application/json",
        async:true,
        data:{
            productId:productId,
        },
        success:function (data) {
            console.log(data)
            if(data=="1"){
                n = index  //调度按钮的下标  用于调度以后修改按钮的innerHtml值
                id=i
                $("#shouldAmount").val("")  //把之前的数据清空
                number = numbe
                pro = productId
                $.ajax({
                    url:"/inputmanagerController/querySpaysCellCurrentAmount",
                    type:"get",
                    contentType: "application/json",
                    async:true,
                    data:{
                        productId:productId,
                    },
                    success:function (data) {
                        console.log(data)
                        $("#mayAmount").html(data)
                    }
                })
            }
            if(data=="-1"){
                alert("对不起，该产品尚未分配存储空间，请返回")
                return;
            }
            if(data=="0"){
                alert("该产品分配存储空间尚未审核")
                return;
            }
            if(data=="2"){
                alert("该产品分配存储空间尚未通过")
                return;
            }
        }
    })


}